package com.imessage.service.impl;

import com.bean.*;
import com.imessage.service.BaseService;
import com.imessage.service.InitService;
import com.imessage.type.*;
import com.imessage.utils.MD5Encrypt;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class InitServiceImpl implements InitService {

    @Autowired
    private Environment environment;

    @Autowired
    private BaseService baseService;

    @Override
    public void initMenu() {
        List<Expression> expressionList = new ArrayList<Expression>();
        List<ManageResource> manageResourceList =
                baseService.findList(ManageResource.class, expressionList);
        if (manageResourceList != null
                && manageResourceList.size() > 0) {
            return;
        }

/*** 任务管理  start ***/
        ManageResource manageMenu100 = getManageResource(
                "任务管理", null, 100,
                ManageResourceType.path, null);
        manageMenu100.save();
        ManageResource manageMenu100001 = getManageResource(
                "新增消息", "/admin/sendtask/initpage?req_id=", 100001,
                ManageResourceType.menu, manageMenu100);
        manageMenu100001.save();
        ManageResource manageMenu100002 = getManageResource(
                "发送任务列表", "/admin/business/sendtask/list.html", 100002,
                ManageResourceType.menu, manageMenu100);
        manageMenu100002.save();

        ManageResource manageMenu100003 = getManageResource(
                "其它任务列表", "/admin/business/othertask/list.html", 100003,
                ManageResourceType.menu, manageMenu100);
        manageMenu100003.save();
/*** 任务管理  end***/

/*** 设备管理  start ***/
        ManageResource manageMenu200 = getManageResource(
                "设备管理", null, 200, ManageResourceType.path, null);
        manageMenu200.save();
        ManageResource manageMenu200001 = getManageResource(
                "设备管理", "/admin/device/client/list.html", 200001,
                ManageResourceType.menu, manageMenu200);
        manageMenu200001.save();
        ManageResource manageMenu200002 = getManageResource(
                "虚拟机管理", "/admin/device/virtual/list.html", 200002,
                ManageResourceType.menu, manageMenu200);
        manageMenu200002.save();
/*** 设备管理  end***/

        ManageResource manageMenu300 = getManageResource(
                "数据管理", null, 300,
                ManageResourceType.path, null);
        manageMenu300.save();
        ManageResource manageMenu300001 = getManageResource(
                "五码管理", "/admin/business/wuma/list.html", 300001,
                ManageResourceType.menu, manageMenu300);
        manageMenu300001.save();
        ManageResource manageMenu300002 = getManageResource(
                "产品管理", "/admin/base/product/list.html", 300001,
                ManageResourceType.menu, manageMenu300);
        manageMenu300002.save();
        ManageResource manageMenu300003 = getManageResource(
                "APPID管理", "/admin/business/apple/list.html", 300003,
                ManageResourceType.menu, manageMenu300);
        manageMenu300003.save();
//****

        ManageResource manageMenu400 = getManageResource(
                "号码管理", null, 400,
                ManageResourceType.path, null);
        manageMenu400.save();
        ManageResource manageMenu400001 = getManageResource(
                "分组管理", "/admin/no/group/list.html", 400001,
                ManageResourceType.menu, manageMenu400);
        manageMenu400001.save();
        ManageResource manageMenu400002 = getManageResource(
                "正常号码", "/admin/no/usephone/list.html", 400002,
                ManageResourceType.menu, manageMenu400);
        manageMenu400002.save();
        ManageResource manageMenu400003 = getManageResource(
                "异常号码", "/admin/no/errorphone/list.html", 400003,
                ManageResourceType.menu, manageMenu400);
        manageMenu400003.save();
//*****
        ManageResource manageMenu900 = getManageResource(
                "系统管理", null, 900,
                ManageResourceType.path, null);
        manageMenu900.save();
        ManageResource manageMenu900001 = getManageResource(
                "菜单管理", "/admin/manage/menu/list.html", 900001,
                ManageResourceType.menu, manageMenu900);
        manageMenu900001.save();
        ManageResource manageMenu900002 = getManageResource(
                "角色管理", "/admin/manage/role/list.html", 900002,
                ManageResourceType.menu, manageMenu900);
        manageMenu900002.save();
        ManageResource manageMenu900003 = getManageResource(
                "用户管理", "/admin/manage/user/list.html", 900003,
                ManageResourceType.menu, manageMenu900);
        manageMenu900003.save();
        ManageResource manageMenu900004 = getManageResource(
                "参数管理", "/admin/base/config/list.html", 900004,
                ManageResourceType.menu, manageMenu900);
        manageMenu900004.save();
    }

    private ManageResource getManageResource(String name,
                                             String url, long sort, ManageResourceType type,
                                             ManageResource parentManageResource) {
        ManageResource manageResource = new ManageResource();
        manageResource.setName(name);
        manageResource.setType(type);
        manageResource.setSort(sort);
        if (StringUtils.isNotBlank(url)) {
            manageResource.setUrl(url);
        }
        if (parentManageResource != null) {
            manageResource.setManageHighResourceId(parentManageResource.getId());
        } else {
            manageResource.setManageHighResourceId(0l);
        }
        manageResource.setState(BaseState.normal);
        manageResource.setHasHide(ManageResourceHasHide.hidden);
        return manageResource;
    }


    @Override
    public void initUser() {
        ManageUser manageUser = baseService.findObject(ManageUser.class,
                Expr.eq("name", "admin"));
        if (manageUser != null) {
            return;
        }
        manageUser = new ManageUser();
        manageUser.setPasswd(MD5Encrypt.MD5("123456"));
        manageUser.setName("admin");
        manageUser.setState(BaseState.normal);
        manageUser.save();
        ManageRole manageRole = new ManageRole();
        manageRole.setCreatedTime(new Date());
        manageRole.setName("管理员角色");
        manageRole.setState(BaseState.normal);
        manageRole.save();
        List<Expression> expressionList = new ArrayList<Expression>();
        List<ManageResource> manageMenuList = baseService.findList(ManageResource.class, expressionList);
        for (Object o : manageMenuList) {
            ManageResource manageMenu = (ManageResource) o;
            if (manageMenu.getManageHighResourceId() == 0) {
                continue;
            }
            ManageResourceRole manageRoleMenu = new ManageResourceRole();
            manageRoleMenu.setCreatedTime(new Date());
            manageRoleMenu.setManageResourceId(manageMenu.getId());
            manageRoleMenu.setManageRoleId(manageRole.getId());
            manageRoleMenu.save();
        }
        ManageRoleUser manageUserRole = new ManageRoleUser();
        manageUserRole.setManageRoleId(manageRole.getId());
        manageUserRole.setManageUserId(manageUser.getId());
        manageUserRole.save();
    }

    @Override
    public void initConfig() {
        BaseConfigCode[] baseConfigCodes = BaseConfigCode.values();
        for (BaseConfigCode baseConfigCode : baseConfigCodes) {
            BaseConfig baseConfig = baseService.findObject(BaseConfig.class,
                    Expr.eq("code", baseConfigCode.getCode()));
            if (baseConfig != null) {
                continue;
            }
            saveBaseConfig(baseConfigCode);
        }
    }

    @Override
    public void initGroup() {
        List<Expression> expressionList = new ArrayList<Expression>();
        List<BusinessGroup> businessGroupList =
                baseService.findList(BusinessGroup.class, expressionList);
        if (businessGroupList != null
                && businessGroupList.size() > 0) {
            return;
        }
        BusinessGroup businessGroup = new BusinessGroup();
        businessGroup.setState(BaseState.normal);
        businessGroup.setName("测试组");
        baseService.saveObject(businessGroup);
    }

    private void saveBaseConfig(BaseConfigCode baseConfigCode) {
        BaseConfig baseConfig = new BaseConfig();
        baseConfig.setCode(baseConfigCode);
        baseConfig.setName(baseConfigCode.getDisplay());
        baseConfig.setValue(baseConfigCode.getValue());
        baseConfig.setState(BaseState.normal);
        baseService.saveObject(baseConfig);
    }
}
